﻿Imports System.Data
Imports ADOEntityKYC
Imports ADOEntityAMLO101
Imports ADOEntity

Partial Class AMLO_Amlo1_05_9_CBS
    Inherits Web.UI.BasePage
    Private Const strMenuCode As String = "M58"
    Dim dsAmlo As DataSet
    Dim clsDataTable As clsLINQToDataTable
    Dim AMLOEntity As AMLO101Entities
    Dim intBeforeItemIndex As Integer
    Dim clsSessionMenuRight As clsSessionMenuRight
    Dim clsFormatDate As clsDateFormat
    Dim clsMasterDropDown As clsMasterDropDown

#Region "Even From "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        chkIsLogin()
        btnSearch.Attributes.Add("onclick", "NoDoubleSubmit();")
        If (Not IsPostBack) Then
            AMLOEntity = New AMLO101Entities
            clsMasterDropDown = New clsMasterDropDown
            clsSessionMenuRight = Session("ClsMenuRight")
            dsAmlo = New DataSet()
            CreateControlMP()
            loadDropDown()
            setUserRightButton()
            setDefaultSearch()


            AMLOEntity.Dispose()
            AMLOEntity = Nothing
        Else
            dsAmlo = Session("ds")
            'grdAmlo.DataSource = dsAmlo.Tables("Amlo")
            'grdAmlo.DataBind()
        End If
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("ds") = dsAmlo
    End Sub
#End Region

#Region "SetForm"

    Sub resourceSet()

    End Sub
#End Region

#Region "Even Button"
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        lblMsg.Visible = False
        lblMsg.Text = ""
        IsDataBind()
        SaveLogSearch()
        clearGvIndex()
    End Sub

#End Region

#Region "Event Dropdown"
    Protected Sub ddlDepartment_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDepartment.SelectedIndexChanged
        Dim clsDropDown As New clsMasterDropDown
        clsSessionMenuRight = Session("ClsMenuRight")


        If Not (ddlDepartment.SelectedValue = "0") Then
            clsDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
        Else
            clsDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
        End If

        clsDropDown = Nothing

    End Sub
#End Region


#Region "Even GridView"

    Protected Sub grdAmlo_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdAmlo.PreRender
        If (Not IsNothing(grdAmlo.HeaderRow)) Then
            Dim row = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)
            Dim cell = New TableHeaderCell()
            cell.Text = "ธุรกรรมที่เกิดขึ้นจริง"
            cell.ColumnSpan = 10
            row.Controls.Add(cell)

            cell = New TableHeaderCell()
            cell.Text = ""
            cell.ColumnSpan = 1
            cell.BackColor = Drawing.Color.Black
            cell.BorderColor = Drawing.Color.Black
            row.Controls.Add(cell)

            cell = New TableHeaderCell()
            cell.Text = "ธุรกรรมที่สาขาบันทึก"
            cell.ColumnSpan = 11
            cell.Width = Unit.Percentage(50)
            row.Controls.Add(cell)

            'row.BackColor = Drawing.ColorTranslator.FromHtml("#3AC0F2")
            grdAmlo.HeaderRow.Parent.Controls.AddAt(0, row)
        End If
    End Sub

    Protected Sub grdAmlo_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdAmlo.RowDataBound
        clsFormatDate = New clsDateFormat
        If (e.Row.RowType.ToString = "DataRow") Then
            'If Not (e.Row.Cells(2).Text = Nothing) Then
            '    e.Row.Cells(2).Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, e.Row.Cells(2).Text)
            'End If
            'If Not (e.Row.Cells(10).Text = Nothing) Then
            '    e.Row.Cells(10).Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, e.Row.Cells(10).Text)
            'End If
        End If
    End Sub

    Protected Sub grdAmlo_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdAmlo.PageIndexChanging
        grdAmlo.PageIndex = e.NewPageIndex
        clearGvIndex()
        DataBind()
    End Sub

    Protected Sub grdAmlo_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdAmlo.Sorting
        Dim dt = TryCast(dsAmlo.Tables("Amlo"), DataTable)

        If dt IsNot Nothing Then

            'Sort the data.
            dt.DefaultView.Sort = e.SortExpression & " " & GetSortDirection(e.SortExpression)
            grdAmlo.DataSource = dsAmlo.Tables("Amlo")
            grdAmlo.DataBind()
            clearGvIndex()
        End If
    End Sub

    Protected Sub chkAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim chk As CheckBox = CType(grdAmlo.HeaderRow.FindControl("chkAll"), CheckBox)
        If (chk.Checked) Then
            For i As Int16 = 0 To grdAmlo.Rows.Count - 1
                Dim chkrow As CheckBox = CType(grdAmlo.Rows(i).FindControl("CheckBox1"), CheckBox)
                chkrow.Checked = True

            Next
        Else

            For i As Int16 = 0 To grdAmlo.Rows.Count - 1
                Dim chkrow As CheckBox = CType(grdAmlo.Rows(i).FindControl("CheckBox1"), CheckBox)
                chkrow.Checked = False

            Next

        End If
    End Sub
#End Region

#Region " Function "

    Private Sub SaveLogPrint(ByVal index As Integer)
        Try
            Dim program As String = strMenuCode
            Dim desc As String = ""
            Dim strUnique As String = "033"

            clsSessionMenuRight = Session("ClsMenuRight")

            If index = 1 Then
                desc = "ค้นหาข้อมูลจาก "
                If Not (ddlBranch.SelectedIndex = 0) Then
                    desc = desc & " ส่วนงาน/สาขา " & ddlBranch.Text
                End If


                If Not (txtYear.Text = "") Then
                    desc = desc & " ปี " & txtYear.Text
                End If
                If Not (txtNo.Text = "") Then
                    desc = desc & " เลขที่ " & txtNo.Text
                End If

                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    desc = desc & " วันที่ทำธุรกรรม " & txtDateStart.Text & " ถึง " & txtDateEnd.Text
                End If

                If Not (rdolIsComplete.SelectedValue = -1) Then
                    desc = desc & " สถานะ " & rdolIsComplete.Text
                End If


            Else
                desc = "พิมพ์ แบบรายงานเปรียบเทียบการทำธุรกรรมที่ใช้เงินสด"
            End If

            Dim strFullNo As String = CType(grdAmlo.Rows(grdAmlo.SelectedIndex).FindControl("hdfRptFullNo"), HiddenField).Value


            Dim clsLogTransaction As New clsLogTransaction
            clsLogTransaction.saveLogTransaction(strFullNo.Trim, clsSessionMenuRight.getUserName, index, desc, program)
            clsLogTransaction = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub setUserRightButton()
        Dim col As New Collection

        col.Add(btnSearch, statusbtn.Query.ToString)
        col.Add(btnVerify, statusbtn.Modify.ToString)
        col.Add(btnPrint, statusbtn.print.ToString)
        setBtnToUserRight(col, strMenuCode, clsSessionMenuRight.getDsMenuRight)

    End Sub

    Private Sub loadDropDown()
        clsMasterDropDown = New clsMasterDropDown
        If (clsSessionMenuRight.getRoleGroup > 79) Then
            clsMasterDropDown.setDeptToDdlSearch(ddlDepartment, _
                                       strDeptCode:="0", _
                                       strRoleCode:=clsSessionMenuRight.getRoleGroup)
            clsMasterDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:="0", _
                                            strBranchCode:=clsSessionMenuRight.getBranchCode, _
                                            strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                            strZoneCode:=clsSessionMenuRight.getZoneCode, _
                                            selectData:=clsSessionMenuRight.getBranchCode)

        Else
            clsMasterDropDown.setDeptToDdlSearch(ddlDepartment, _
                                       strDeptCode:=clsSessionMenuRight.getDeptCode, _
                                       strRoleCode:=clsSessionMenuRight.getRoleGroup)
            clsMasterDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=clsSessionMenuRight.getDeptCode, _
                                                        strBranchCode:=clsSessionMenuRight.getBranchCode, _
                                                        strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                                        strZoneCode:=clsSessionMenuRight.getZoneCode, _
                                                        selectData:=clsSessionMenuRight.getBranchCode)

        End If

    End Sub

    Public Sub setDefaultSearch()
        clsFormatDate = New clsDateFormat
        txtDateStart.Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)
        txtDateEnd.Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)
        txtYear.Text = clsFormatDate.GetYearLast(clsDateFormat.enmDateCultureInfo.Th).Substring(2, 2)


        If (clsSessionMenuRight.getRoleGroup > 79) Then
            rdoDelete.Visible = True
            ChkIsDelete.Visible = False
        ElseIf (clsSessionMenuRight.getRoleGroup < 80) Then
            rdoDelete.Visible = False
            ChkIsDelete.Visible = True
        End If

        If (clsSessionMenuRight.getRoleGroup > 59) Then
            ddlBranch.Enabled = True
        ElseIf (clsSessionMenuRight.getRoleGroup < 60) Then
            ddlBranch.Enabled = False
        End If

        rdolIsComplete.SelectedValue = -1
        rdoDelete.SelectedIndex = 0
    End Sub

    Public Sub CreateControlMP() 'อ้าง control ใน masterPage
        Dim img As Image = CType(Master.FindControl("imgHead"), Image)
        img.ImageUrl = "~/img/ghb_.GIF"

        Dim lbl As Label = CType(Master.FindControl("lblPage"), Label)
        lbl.Text = "ตรวจสอบข้อมูล ปปง.1-05-9"
    End Sub

    Private Sub setBranch() 'ตั้งค่าหน้าเว็บ

        Dim qryBranch = From b In AMLOEntity.SBRANCH
                           Select b.BRANCHCODE, b.BRANCHNAME

        ddlBranch.DataSource = qryBranch
        ddlBranch.DataTextField = "BRANCHNAME"
        ddlBranch.DataValueField = "BRANCHCODE"
        ddlBranch.DataBind()
        ddlBranch.Items.Insert(0, "** กรุณาเลือกสาขา **")

        qryBranch = Nothing


    End Sub

    Private Sub IsDataBind()
        clsFormatDate = New clsDateFormat
        Dim UserRight As clsSessionMenuRight
        UserRight = Session("clsMenuRight")
        Dim strSql As String

        If dsAmlo.Tables.Contains("Amlo") Then
            dsAmlo.Tables.Clear()
        End If

        strSql = "select '' as cbs_report_flg, '' as cbs_full_rptno, '' as cbs_mapby, t.* from amlo1059 t "

        'If Not (ddlBranch.SelectedValue = "0") Then
        '    strSql = strSql & " and ( cbs_branchcode = '" & ddlBranch.SelectedValue & "' or branchcode = '" & ddlBranch.SelectedValue & "')"
        'End If

        'If Not (txtDateStart.Text = "" AndAlso txtDateEnd.Text = "") Then
        '    Dim dteStartDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text) & " 00:00:00"
        '    Dim dteEndDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text) & " 23:59:59"
        '    Dim clsCultureInfo = New clsCultureInfo()
        '    Dim strStartDate = clsCultureInfo.EnDate(dteStartDate)
        '    Dim strEndDate = clsCultureInfo.EnDate(dteEndDate)

        '    strSql = strSql & " and ((cbs_tran_date >= to_date('" & strStartDate & "', 'yyyymmdd') and cbs_tran_date <= to_date('" & strEndDate & "', 'yyyymmdd'))"
        '    strSql = strSql & " or (transdate >= to_date('" & strStartDate & "', 'yyyymmdd') and transdate <= to_date('" & strEndDate & "', 'yyyymmdd')))"
        'End If

        Dim clsDatabase = New clsDatabase()
        Dim dt = clsDatabase.QueryDataTable(strSql)
        dt.TableName = "Amlo"
        dsAmlo.Tables.Add(dt.Copy)

        If (dt.Rows.Count = 0) Then
            lblMsg.Visible = True
            lblMsg.Text = "ไม่พบข้อมูล"
            lblTotal.Text = ""
        Else
            lblMsg.Visible = False
            lblMsg.Text = ""
            lblTotal.Text = "จำนวนรายการทั้งหมด " + dt.Rows.Count.ToString + " รายการ "
        End If

        grdAmlo.DataSource = dsAmlo.Tables("Amlo")
        grdAmlo.DataBind()
    End Sub

    Private Sub clearGvIndex()
        grdAmlo.SelectedIndex = -1
        btnVerify.Attributes.Clear()
    End Sub

    Private Function GetSortDirection(ByVal column As String) As String

        ' By default, set the sort direction to ascending.
        Dim sortDirection = "DESC"

        ' Retrieve the last column that was sorted.
        Dim sortExpression = TryCast(ViewState("SortExpression"), String)

        If sortExpression IsNot Nothing Then
            ' Check if the same column is being sorted.
            ' Otherwise, the default value can be returned.
            If sortExpression = column Then
                Dim lastDirection = TryCast(ViewState("SortDirection"), String)
                If lastDirection IsNot Nothing _
                  AndAlso lastDirection = "DESC" Then

                    sortDirection = "ASC"

                End If
            End If
        End If

        ' Save new values in ViewState.
        ViewState("SortDirection") = sortDirection
        ViewState("SortExpression") = column
        Return sortDirection
    End Function

#End Region


    Protected Sub btnVerify_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVerify.Click

        Dim iAllRec = dsAmlo.Tables("Amlo").Rows.Count
        Dim isHasCheck = False
        For iCount = 0 To iAllRec - 1
            Dim chkConfirmGen As CheckBox = CType(grdAmlo.Rows(iCount).FindControl("CheckBox1"), CheckBox)
            If (chkConfirmGen.Checked) Then
                isHasCheck = True
                Exit For
            End If
        Next

        If (isHasCheck) Then
            Dim clsDatabase = New clsDatabase()
            Dim strSql As String

            Dim tempSql = "update amlo101_cbs " _
                & " set isconfirm = 1" _
                & " where seq = {0}"

            Dim tempSql2 = "update amlo101 " _
                & " set is_cbs_confirm = 1" _
                & " where rptseq = {0}"

            For iCount = 0 To iAllRec - 1

                Dim chkConfirmGen As CheckBox = CType(grdAmlo.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                If (chkConfirmGen.Checked) Then
                    Dim cbsSeq As String = CType(grdAmlo.Rows(iCount).FindControl("hdfCbsSeq"), HiddenField).Value
                    Dim rptSeq As String = CType(grdAmlo.Rows(iCount).FindControl("hdfRptSeq"), HiddenField).Value

                    If Not (String.IsNullOrEmpty(cbsSeq)) Then
                        strSql = String.Format(tempSql, cbsSeq)
                        clsDatabase.QueryExecuteNonQuery(strSql)
                    End If

                    If Not (String.IsNullOrEmpty(rptSeq)) Then
                        strSql = String.Format(tempSql2, rptSeq)
                        clsDatabase.QueryExecuteNonQuery(strSql)
                    End If
                End If
            Next

            IsDataBind()
            clearGvIndex()
        Else
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + Resources.resValidate.nonSelectGrid + "')", True)
        End If

    End Sub

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        'Dim txtCode As String = CType(grdAmlo.Rows(grdAmlo.SelectedIndex).FindControl("hdfRptSeq"), HiddenField).Value
        Dim param As String = "{ 'RPTTYPE' : '" & strMenuCode & "'," _
                                    & "'BRANCHCODE' : '" & ddlBranch.SelectedValue & "'," _
                                    & "'STARTDATE' : '" & txtDateStart.Text.Trim & "'," _
                                    & "'ENDDATE' : '" & txtDateEnd.Text.Trim & "'}"
        'SaveLogPrint(2)

        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", "OpenWindowWithPost('../Form/frmReportAmloCBS.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)

    End Sub

    Private Sub SaveLogSearch()
        Try
            clsSessionMenuRight = Session("ClsMenuRight")
            Dim desc As String = ""



            If Not (ddlBranch.SelectedValue = "0") Then
                desc += " ส่วนงาน/สาขา - " + ddlBranch.SelectedItem.Text
            End If

            If Not (txtDateStart.Text = "") And Not (txtDateEnd.Text = "") Then
                desc += " วันที่ทำธุรกรรม - " + txtDateStart.Text + " ถึง " + txtDateEnd.Text
            End If

            If Not (txtYear.Text = "") Then
                desc += " ปี - " + txtYear.Text
            End If

            If Not (txtNo.Text = "") Then
                desc += " เลขที่ - " + txtNo.Text
            End If

            If rdolIsComplete.SelectedValue = 0 Then
                desc += " สถานะ - " + "ทั้งหมด "
            ElseIf rdolIsComplete.SelectedValue = 1 Then
                desc += " สถานะ - " + "ครบถ้วน "
            ElseIf rdolIsComplete.SelectedValue = 2 Then
                desc += " สถานะ - " + "ไม่ครบถ้วน "
            End If




            If Not desc = "" Then
                desc = "ค้นหาข้อมูลผู้ใช้งานระบบ จาก " + desc
            Else
                desc = "ค้นหาทั้งหมด "
            End If

            If rdoDelete.Visible = True Then
                If rdolIsComplete.SelectedValue = 0 Then
                    desc += " สถานะการลบ - " + "ทั้งหมด "
                ElseIf rdolIsComplete.SelectedValue = 1 Then
                    desc += " สถานะการลบ - " + "ขอลบ "
                ElseIf rdolIsComplete.SelectedValue = 2 Then
                    desc += " สถานะการลบ - " + "ยืนยันการลบ "
                End If
            End If

            If ChkIsDelete.Visible = True Then
                If ChkIsDelete.Checked = True Then
                    desc += " สถานะการลบ - " + "ขอลบ "
                End If
            End If

            Dim clsLogTransaction As New clsLogTransaction
            clsLogTransaction.saveLogTransaction("", clsSessionMenuRight.getUserName, 1, desc, strMenuCode)
            clsLogTransaction = Nothing
        Catch ex As Exception
            Throw ex
        End Try

    End Sub




End Class
